package com.cuit.utils;

import java.util.LinkedHashSet;

public class HandlePasswordUtil {
    /**
     * 对密码进行校验判断，弱密码将返回true，强密码返回false
     *
     * 如果密码是以下几种情况将返回true,即是弱密码：
     * 1.如果密码是admin、
     * 2.密码位数小于等于6位、
     * 3.密码全部是相同的字符（如：aaaaaaa、8888888）、
     * 4.密码全部是空字符、
     * 5.密码全是顺序字符（如：123456、987654、bcdefg、gfedcb）
     */
    public static boolean isWeekPassword(String password) {
        password = password.trim();
        //判断密码是不是特殊的admin
        if (password.length() <= 8) {
            return true;
        }
        char[] chPassword = password.toCharArray();
        LinkedHashSet<Character> link = new LinkedHashSet<>();
        StringBuilder buff = new StringBuilder();
        boolean result = false;
        int count = 0;
        for (int i = 0; i < chPassword.length; i++) {
            //判断密码是否是连续的正序或降序数字或字母
            int preNumber = i == 0 ? -999 : chPassword[i - 1] - 0; //前一字符对应的ascii码
            int currentNumber = chPassword[i] - 0;//当前字符对应的ascii码
            int asciiNum = preNumber + currentNumber;
            if ((96 <= asciiNum && asciiNum <= 114) || (130 <= asciiNum && asciiNum <= 180) || (194 <= asciiNum && asciiNum <= 244)) {
                //判断密码的相邻字符是否是正序或降序的数字或字符
                if (Math.abs(preNumber - currentNumber) == 1) {
                    count += 1;
                }
            }
            //处理不重复的字符
            if (!link.contains(chPassword[i])) {
                link.add(chPassword[i]);
                buff.append(chPassword[i]);
            }
        }
        //处理空字符串和密码全是同一字符的
        String resultStr = String.valueOf(buff);
        if (count == chPassword.length - 1 || resultStr.length() <= 1) {
            result = true;
        }
        return result;
    }
}
